//
//  YMStationAnnotationView.m
//  iOS_movingAnnotation
//
//  Created by lantaiyuan on 2017/10/18.
//  Copyright © 2017年 yours. All rights reserved.
//

#import "YMStationAnnotationView.h"
#import <Masonry.h>
#import "YMTravelTrack.h"

#define kWidth  100.f
#define kHeight 70.f

@interface YMStationAnnotationView()
@property(nonatomic, strong) UILabel *stationLabel;
@property(nonatomic, strong) UILabel *timeLabel;
@property(nonatomic, strong) UILabel *peopleLabel;
@property(nonatomic, strong) UIView *lineView;

@property(nonatomic, assign) CGFloat ym_height;
@property(nonatomic, assign) CGFloat ym_width;
@end

@implementation YMStationAnnotationView

- (void)drawRect:(CGRect)rect{
    // 默认圆角角度
    float radius = 4;
    // 居中偏移量(箭头高度)
    float offset = 5;
    // 设置 箭头位置
    float positionNum = self.frame.size.width / 2.0;
    
    // 定义坐标点 移动量
    float changeNum = radius + offset*2;
    // 设置画线 长 宽
    float w = self.frame.size.width ;
    float h = self.frame.size.height;
    
    // 获取文本
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置 边线宽度
    CGContextSetLineWidth(context, 0.5);
    //边框颜色
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"#e8e8e8"].CGColor);
    //矩形填充颜色
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    CGContextMoveToPoint(context, radius, 0);  // 开始坐标左边开始
    CGContextAddArcToPoint(context, w, 0, w, changeNum, radius);  // 右上角角度
    CGContextAddArcToPoint(context, w , h - offset*3, w - changeNum, h - offset*3, radius);  // 右下角角度
    
    CGContextAddLineToPoint(context, positionNum + 5, h - offset*3); // 向左划线
    CGContextAddLineToPoint(context, positionNum, h - offset*2); // 向下斜线
    CGContextAddLineToPoint(context, positionNum - 5, h - offset*3); // 向上斜线
    
    CGContextAddArcToPoint(context, 0, h - offset*3, 0, h - changeNum - offset, radius); // 左下角角度
    CGContextAddArcToPoint(context, 0, 0, radius, 0, radius); // 左上角角度
    
    CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
    
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"#5298ff"].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextAddArc(context, w/2, h - offset, 4.0, 0, 2*M_PI, 0);
    
    CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
    
    /** 父类调用 放在画完边线 后.  不然 设置的文字会被覆盖 */
    [super drawRect:rect];
}

- (void)setStation:(YMStation *)station{
    self.stationLabel.text = station.stationName;
    self.peopleLabel.text = [NSString stringWithFormat:@"上%ld人,下%ld人",(long)[station.upNum integerValue],[station.downNum integerValue]];
    self.timeLabel.text = [self timeWithTimeIntervalString:[NSString stringWithFormat:@"%@",station.arriveTime]];
    [self test:station];
}

- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    self.centerOffset = CGPointMake(0, -self.ym_height/2.0);
    self.backgroundColor = [UIColor clearColor];
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, self.ym_width, self.ym_height);
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.font = [UIFont systemFontOfSize:11];
        self.timeLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        [self addSubview:self.timeLabel];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor colorWithHexString:@"#e8e8e8"];
        [self addSubview:self.lineView];
        
        self.stationLabel = [[UILabel alloc] init];
        self.stationLabel.font = [UIFont systemFontOfSize:11];
        self.stationLabel.textColor = [UIColor colorWithHexString:@"#5298ff"];
        [self addSubview:self.stationLabel];
        
        self.peopleLabel = [[UILabel alloc] init];
        self.peopleLabel.font = [UIFont systemFontOfSize:11];
        self.peopleLabel.textColor = [UIColor colorWithHexString:@"#5298ff"];
        self.peopleLabel.text = @"上30人,下30人";
        [self addSubview:self.peopleLabel];
    }
    
    return self;
}

- (void)test:(YMStation *)station{

    NSString * str = [self.timeLabel.text stringByAppendingString:self.peopleLabel.text];
    self.width = [self getWidthWithString:str] + 35;
    
    __weak __typeof(self)weakSelf = self;
    CGFloat ym_timeW = [self getWidthWithString:@"99::99"];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@7);
        make.centerY.mas_equalTo(weakSelf).offset(-5);
        make.width.mas_equalTo(ym_timeW);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeLabel.mas_right).offset(6);
        make.top.equalTo(@5);
        make.bottom.equalTo(@-20);
        make.width.mas_equalTo(0.5);
    }];
    
    CGFloat ym_stationH = [self getHeightWithString:@"上30人,下30人"];
    [self.stationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeLabel.mas_right).offset(12);
        make.top.equalTo(@6);
        make.right.equalTo(@-10);
        make.height.mas_equalTo(ym_stationH);
    }];
    
    [self.peopleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.stationLabel.mas_left);
        make.top.equalTo(weakSelf.stationLabel.mas_bottom).offset(7);
        make.right.equalTo(@-10);
        make.height.mas_equalTo(ym_stationH);
    }];
}

- (CGFloat)ym_width{
    CGSize size =  [@"99:99上30人,下30人" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{} context:nil].size;
    return size.width + 25;
}

- (CGFloat)ym_height{
   CGSize size =  [@"99:99上30人,下30人" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{} context:nil].size;
    
    return size.height*2 +7*3 + 5*3;
}

- (CGFloat)getWidthWithString:(NSString *)string{
    CGSize size =  [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{} context:nil].size;
    return size.width;
}

- (CGFloat)getHeightWithString:(NSString *)string{
    CGSize size =  [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{} context:nil].size;
    return size.height;
}

@end
